GBML manual / Grids

grid_sort_rows

Puts the ROWS of target in order by the value each one has in one column, and keeps every row together: the cells of a row move as a whole, so a name stays beside its score. by_column says which column to look at (0 is the left-most) and ascending picks smallest first (true) or biggest first (false). Rows that have the same value in that column stay in the order they were in. The grid must hold numbers or text, and the value in that column is what is compared. It changes target itself and makes no copy.

Syntax

grid_sort_rows(target, by_column, ascending)

Arguments

NameType
targetgrid
by_columnint
ascendingbool

Returns

Nothing.

Description

Puts the rows of target, a grid, in order by the value each row has in one column. Every row moves as a whole, so the cells of a row stay together. That is what a high-score table wants: sort by the score and the player's number comes along with it.

by_column says which column to look at. The left-most column is 0. ascending picks the direction: true puts the smallest value on the top row, false puts the biggest on the top row.

grid_sort_rows(table, 1, false);   // look at column 1 (the score), biggest on top

Here is a table with two columns, the player's number and their score, before and after grid_sort_rows(table, 1, false):

Before                  After
player  score           player  score
  1      50               2      90
  2      90               4      90
  3      70               3      70
  4      90               1      50

The rows came out in order of score, biggest first, and every player is still beside their own score. grid_sort is the other one: it puts each column in order on its own, so it would have shuffled the players and the scores apart. Use grid_sort_rows whenever a row means something as a whole.

Rows with the same value stay in the order they had

Players 2 and 4 both scored 90. After the sort player 2 is still above player 4, because that is how they were before. A row never jumps over another row that has the same value in the column. That is true going up and going down, so a tie in a high-score table is won by whoever got there first.

Numbers and text

The cells of a grid all hold the same kind of value, and the value in the column you pick is the one that is compared. So the grid decides how the rows are put in order:

The grid holdsWhat happens
whole numbers (grid<int>) or decimals (grid<real>)the rows go in number order
text (grid<string>)the rows go in alphabetical order of the text in that column, in the order below
yes-or-no values, structs or referencesrefused when checked, before the game runs

Because a grid holds one kind of value, a table cannot have a name in one column and a score in the next. Keep the player's number in the grid, as above, and keep the names in a list or array, then find a name with the number: names[table[# 0, 0]]. A grid of text is for things like a table of words and their meanings. Do not keep scores in it: as text, "9" comes after "10".

Text goes in alphabetical order, with a catch

Text is put in order one character at a time, by each character's code. The code is the number a character has in the table of letters ("A" is 65, "a" is 97, "0" is 48, a space is 32: see string_code_at). That gives these rules:

RuleExample
a space comes before a digit, a digit before a capital letter, a capital letter before a small letter" x", "10", "Zed", "ada" come in that order
every capital letter comes before every small letter. A capital and a small letter are not treated as the same letter"Zed" comes before "ada", and "Apple" before "apple"
digits are compared one at a time"10" comes before "9", because 1 comes before 9
a text that is the start of a longer one comes first"ab" comes before "abc"
a character past the plain keyboard ones (a code above 127) comes after all of themit comes last

If you want names to come out the way a dictionary would put them, write them all with a capital first letter, or all small.

Which column

by_column has to be a column the grid has: from 0 to one less than grid_width. A number typed straight into the call that the grid does not have, like 3 on a grid that is 3 columns wide, is refused when the project is checked, because it is almost always a mistake. A column that is worked out while the game runs (a variable, a sum) cannot be checked beforehand, so it is cut to the grid instead: a number below 0 sorts by the first column and a number past the end sorts by the last one. Nothing outside the grid is ever looked at.

The grid has to know its size

To find a row, GBML needs to know how many cells are in it. A grid only knows its width and height where it was made (grid_create(8, 6, 0), /// @capacity 8x6, or grid<int, 8, 6>). A grid with no size written like that, or a script's grid parameter, has none to give, and the call is refused, the same as grid_width is.

What happens in the awkward cases

You do thisWhat happens
sort a grid with one row, or with one cellnothing changes
sort a table that is already in orderit is left exactly as it is
leave out by_column or ascending, give a decimal or true for the column, or give a number for ascendingrefused when checked. It needs all three arguments
give a column number the grid does not have, typed straight inrefused when checked
give a list or an arrayrefused when checked, and the message lists what that kind can do, like list_sort for a list
use it where a value is neededrefused when checked. It gives nothing back

What it costs

Nothing is set aside: no copy of the grid and no room for a spare row, because two rows trade places one cell at a time. The sort is an insertion sort. A table that is already in order costs one comparison for each row after the first. A table in exactly the wrong order costs the most: rows × (rows − 1) ÷ 2 comparisons, which is 45 for 10 rows and 4,950 for 100, and every swap moves a whole row's width of cells. That is nothing for a high-score table or an inventory of a few dozen rows. Sorting a table of hundreds of rows every frame is something to avoid. No timing was measured.

Example

var table = grid_create(2, 4, 0);
table[# 0, 0] = 1;
table[# 1, 0] = 50;
table[# 0, 1] = 2;
table[# 1, 1] = 90;
table[# 0, 2] = 3;
table[# 1, 2] = 70;
table[# 0, 3] = 4;
table[# 1, 3] = 90;
grid_sort_rows(table, 1, false);   // the rows are now 2/90, 4/90, 3/70, 1/50
var top_player = table[# 0, 0];    // 2
var top_score = table[# 1, 0];     // 90

On the GBA

An insertion sort over the row numbers, done in place: each row, from the second down, is compared with the row above it by the cell in column by_column, and while the row above belongs below it the two rows are swapped. A swap trades the two rows cell by cell through one spare cell, so no room for a whole row is needed - it uses no extra memory at all, makes no copy, and never leaves the grid. It only swaps rows that are strictly out of order, so it is stable: two rows with equal values in that column never change places. The time grows with the square of the number of rows in the worst case (a table already in order costs one comparison per row), and each swap moves a whole row's width of cells; that is nothing for a table of a few dozen rows, but sorting a table of hundreds of rows every frame is something to avoid. No timing was measured. by_column is worked out once and held to 0..width-1, so the sort never looks outside a row; a number typed straight into the call that is off the grid is refused when the project is checked. It needs the grid's width and height, which the checker reads from the declaration (`grid_create(8, 6, 0)`, `/// @capacity 8x6` or `grid<int, 8, 6>`) and writes into the C++ as plain numbers; a grid with none, such as a script's parameter, is refused, the same as grid_width. Whole numbers, decimals and text build. Text is put in order one character at a time, by each character's code. A space comes before a digit, a digit before a capital letter and a capital letter before a small one, so "Zed" comes before "ada" (a capital and a small letter are NOT treated as the same letter), and a text that is the start of a longer one comes first ("ab" before "abc"). A character with a code above 127 comes after all the plain keyboard ones. There is no order for yes-or-no values, structs or references that GBML knows, so those are refused when the project is checked.

See also: grid_sortgrid_shuffle_rowslist_sortarray_sortgrid_widthgrid_heightgrid_copystring_code_atGridsData containers